package com.zhongx.xingcd.zxxcd.common;


import com.zhongx.xingcd.zxxcd.service.TrickInfoService;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class ExcelUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelUtils.class);
    @Autowired
    private TrickInfoService trickInfoService;
    public static List excelToShopIdList(InputStream inputStream) {
        List list = new ArrayList<>();
        Workbook workbook = null;
        try {
            workbook = WorkbookFactory.create(inputStream);
            inputStream.close();
            //工作表对象
            Sheet sheet = workbook.getSheetAt(0);
            //总行数
            //int rowLength = sheet.getLastRowNum() + 1;
            int rowLength = sheet.getLastRowNum() + 1;
            //工作表的列
            Row row = sheet.getRow(0);
            //总列数
            int colLength = row.getLastCellNum();
            //得到指定的单元格
            //Cell cell = row.getCell(0);
            for (int i = 1; i < rowLength; i++) {
                Map map = new HashMap();
                row = sheet.getRow(i);
                //for (int j = 0; j < colLength; j++) {
                Cell cell1 = row.getCell(0);
                Cell cell2 = row.getCell(1);
                Cell cell3 = row.getCell(2);
                    if (cell1 != null) {
                        //cell1.setCellType(Cell.CELL_TYPE_STRING);
                        String data = cell1.getStringCellValue();
                        data = data.trim();
                        map.put("ticket_number",data);
                    }
                if (cell2 != null) {
                    //cell2.setCellType(Cell.CELL_TYPE_STRING);
                    String data = cell2.getStringCellValue();
                    data = data.trim();
                    map.put("ticket_money",data);
                }
                if (cell3 != null) {
                    //cell3.setCellType(Cell.CELL_TYPE_STRING);
                    String data = cell3.getStringCellValue();
                    data = data.trim();
                    map.put("ticket_style",data);
                }
                list.add(map);
               // }
            }
        } catch (Exception e) {
            LOGGER.error("parse excel file error :", e);
        }
        return list;
    }


    /**
     * 判断是否是国际或者国内
     * @param list
     * @return
     */

    public static boolean judgeRepeatExcle(List list){
       // boolean flag=false;
        List halist = new ArrayList();
            for(int i =0;i<list.size();i++){
                Map map = new HashMap();
                map = (Map) list.get(i);
                String trname = (String) map.get("ticket_style");
                if("国内".equals(trname)||"国际".equals(trname)){
                    return false;
                }

            }
        return true;
    }


    /**
     * 判断是否有重复
     * @param list
     * @return
     */

    public static boolean shorecontroy(List list){
        // boolean flag=false;
        List halist = new ArrayList();
        for(int i =0;i<list.size();i++){
            Map map = new HashMap();
            map = (Map) list.get(i);
            String trname = (String) map.get("ticket_number");
            halist.add(trname);
        }


        HashSet<String> hashSet = new HashSet<>(halist);
        if (halist.size() != hashSet.size()) {
            System.out.println("list中存在重复的数据");
            System.out.println(halist.size());
            System.out.println(hashSet.size());
            return true;
        }
        return false;
    }
}
